<script setup lang="ts">
import KnowledgeCard from './KnowledgeCard.vue'
import { ref } from 'vue'
import { getKnowledgePage } from '@/api/consult'
import type { KnowledgeList, KnowledgeParams, KnowledgeType } from '@/types/consult'

const props = defineProps<{
  type: KnowledgeType
}>()
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1, // 请求页码
  pageSize: 8 // 每页多少条数据
})
// 获取文章列表
const list = ref<KnowledgeList>([])
const loading = ref(false)
const finished = ref(false)

// 页面打开后默认执行一次，每次用户滚动到底会再次执行：通过loading控制
const onLoad = async () => {
  const res = await getKnowledgePage(params.value)
  list.value.push(...res.rows)
  console.log(params.value.current)
  // 判断数据是否加载完毕：1. list长度===数据总条数；2. current===pageTotal总页数
  if (params.value.current >= res.pageTotal) {
    finished.value = true
  } else {
    params.value.current++
  }
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card v-for="item in list" :key="item.id" :item="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
